package com.ruoyi.develop.goodsSalebuy.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysArea;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
        import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import javax.annotation.Resource;

import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.goodsOrderIn.domain.GoodsOrderIn;
import com.ruoyi.develop.goodsSalebuyall.domain.GoodsSalebuyall;
import com.ruoyi.develop.goodsSalebuyall.mapper.GoodsSalebuyallMapper;
import com.ruoyi.develop.goodsSalebuyall.service.impl.GoodsSalebuyallServiceImpl;
import com.ruoyi.develop.goodsSaledo.domain.GoodsSaledo;
import com.ruoyi.develop.goodsSaledo.mapper.GoodsSaledoMapper;
import com.ruoyi.develop.lvRukumingxi.domain.LvRukumingxi;
import com.ruoyi.develop.lvRukumingxi.service.ILvRukumingxiService;
import com.ruoyi.develop.projectBaseInfo.domain.ProjectBaseInfo;
import com.ruoyi.develop.projectBaseInfo.service.IProjectBaseInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.goodsSalebuy.mapper.GoodsSalebuyMapper;
import com.ruoyi.develop.goodsSalebuy.domain.GoodsSalebuy;
import com.ruoyi.develop.goodsSalebuy.service.IGoodsSalebuyService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 采购单Service业务层处理
 * @author qh
 * @date 2024-08-09
 */
@Service
public class GoodsSalebuyServiceImpl implements IGoodsSalebuyService {
    @Resource
    private GoodsSalebuyMapper goodsSalebuyMapper;
    @Resource
    private GoodsSaledoMapper goodsSaledoMapper;
    @Resource
    private GoodsSalebuyallMapper goodsSalebuyallMapper;
    @Resource
    private GoodsSalebuyallServiceImpl goodsSalebuyallService;
    @Resource
    private IProjectBaseInfoService projectBaseInfoService;

    @Resource
    private ILvRukumingxiService rukumingxiService;



    /**
     * 查询采购单
     * @param id 采购单主键
     * @return 采购单
     */
    @Override
    public GoodsSalebuy selectById(String id) {
        return goodsSalebuyMapper.selectById(id);
    }

    /**
     * 查询采购单列表
     * @param entity 采购单
     * @return 采购单
     */
    @Override
    public List<GoodsSalebuy> selectList(GoodsSalebuy entity) {
        return goodsSalebuyMapper.selectList(entity);
    }
    @Override
    public List<GoodsSalebuy> listByQinggou(GoodsSalebuy entity) {
        return goodsSalebuyMapper.listByQinggou(entity);
    }
    @Override
    public List<GoodsSalebuy> listByCgallid(GoodsSalebuy entity) {
        return goodsSalebuyMapper.listByCgallid(entity);
    }

    /**
     * 新增采购单
     * @param entity 采购单
     * @return 结果
     */
    @Override
    public int insert(GoodsSalebuy entity) {
        if(StringUtils.isBlank(entity.getId())){
            entity.setId(IdUtils.fastSimpleUUID());
        }
                entity.setCreateBy(SecurityUtils.getUsername());
                entity.setCreateTime(DateUtils.getNowDate());
            return goodsSalebuyMapper.insert(entity);
    }

    /**
     * 修改采购单
     * @param entity 采购单
     * @return 结果
     */
    @Override
    public int update(GoodsSalebuy entity) {
                entity.setUpdateBy(SecurityUtils.getUsername());
                entity.setUpdateTime(DateUtils.getNowDate());
        return goodsSalebuyMapper.update(entity);
    }

    @Override
    public int updateGoodsSalebuyBuruku(String mainId,String daohuoStatus) {
        return goodsSalebuyMapper.updateGoodsSalebuyBuruku(mainId,daohuoStatus);
    }


    /**
     * 批量删除采购单
     * @param ids 需要删除的采购单主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return goodsSalebuyMapper.deleteByIds(ids);
    }

    /**
     * 删除采购单信息
     * @param id 采购单主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return goodsSalebuyMapper.deleteById(id);
    }

    @Override
    public List<GoodsSalebuy> selectByCgallId(String cgallid){
        return goodsSalebuyMapper.selectByCgallId(cgallid);
    };

    @Override
    @Transactional
    public void insertBatch(GoodsSaledo goodsSaledo) {
        List<GoodsSaledo> rows = goodsSaledo.getColumns();
        if (!rows.isEmpty()) {
            for (GoodsSaledo entity : rows) {
                entity.setBuyRemain(entity.getBuyRemain().subtract(entity.getBuyCount()));
                goodsSaledoMapper.update(entity);
                GoodsSalebuyall goodsSalebuyall=goodsSalebuyallMapper.selectByGoodsIdAndRemark(entity.getGoodsId(),entity.getRemark(),SecurityUtils.getUsername());
                if(goodsSalebuyall!=null){
                    goodsSalebuyall.setShengyu(entity.getBuyCount().add(goodsSalebuyall.getShengyu()));
                    goodsSalebuyall.setCounts(entity.getBuyCount().add(goodsSalebuyall.getCounts()));
                    if(entity.getDun()!=null && goodsSalebuyall.getDun()!=null) {
                        goodsSalebuyall.setDun(entity.getDun().add(goodsSalebuyall.getDun()));
                    }
                    goodsSalebuyall.setQinggouId(entity.getId());
                    goodsSalebuyall.setProjectName(entity.getGongchengName());
                    goodsSalebuyall.setGoodsUnitPrice(entity.getGoodsUnitPrice());
                    goodsSalebuyall.setMainId(null);
                    goodsSalebuyall.setGoodsType(entity.getAddGoodsType());
                    goodsSalebuyall.setCreateTime(DateUtils.getNowDate());
                    if(goodsSalebuyall.getGoodsBuyTotal()!=null && entity.getGoodsBuyTotal()!=null){
                        goodsSalebuyall.setGoodsBuyTotal(entity.getBuyCount().add(goodsSalebuyall.getGoodsBuyTotal()));
                    }
                    if(goodsSalebuyall.getGoodsTotalPrice()!=null && entity.getGoodsTotalPrice()!=null){
                        goodsSalebuyall.setGoodsTotalPrice(entity.getGoodsTotalPrice().add(goodsSalebuyall.getGoodsTotalPrice()));
                    }
                    goodsSalebuyall.setSaleDate(entity.getSaleDate());
                    goodsSalebuyallService.update(goodsSalebuyall);
                }else{
                    goodsSalebuyall=new  GoodsSalebuyall();
                    goodsSalebuyall.setId(IdUtils.fastSimpleUUID());
                    BeanUtils.copyProperties(entity, goodsSalebuyall, "id");
                    goodsSalebuyall.setSaleDate(entity.getSaleDate());
                    goodsSalebuyall.setProjectName(entity.getGongchengName());
                    goodsSalebuyall.setQinggouId(entity.getId());
                    goodsSalebuyall.setMainId(null);
                    goodsSalebuyall.setStatus("5");
                    goodsSalebuyall.setGoodsType(entity.getAddGoodsType());
                    goodsSalebuyall.setShengyu(entity.getBuyCount());
                    goodsSalebuyall.setCounts(entity.getBuyCount());
                    goodsSalebuyall.setCreateTime(DateUtils.getNowDate());
                    goodsSalebuyall.setGoodsBuyTotal(entity.getBuyCount());
                    goodsSalebuyall.setGoodsTotalPrice(entity.getBuyCount().multiply(entity.getGoodsBuyPrice()));
                    goodsSalebuyallService.insert(goodsSalebuyall);
                }
                GoodsSalebuy goodsSalebuy =new GoodsSalebuy();
                goodsSalebuy.setId(IdUtils.fastSimpleUUID());
                BeanUtils.copyProperties(entity, goodsSalebuy, "id");
                goodsSalebuy.setQinggouId(entity.getId());
                goodsSalebuy.setMainId(null);
                goodsSalebuy.setCgallid(goodsSalebuyall.getId());
                goodsSalebuy.setStatus("5");
                goodsSalebuy.setShengyu(entity.getBuyCount());
                goodsSalebuy.setSaleDate(entity.getSaleDate());
                goodsSalebuy.setProjectName(entity.getGongchengName());
                goodsSalebuy.setGoodsBuyPrice(entity.getGoodsBuyPrice());
                goodsSalebuy.setGoodsUnitPrice(entity.getGoodsUnitPrice());
                goodsSalebuy.setGoodsBuyTotal(entity.getBuyCount().multiply(entity.getGoodsBuyPrice()));
                insert(goodsSalebuy);
            }
        }
    }

    @Override
    @Transactional
    public void insertBuyBatch(GoodsSaledo goodsSaledo) {
        List<GoodsSaledo> rows = goodsSaledo.getColumns();
        if (!rows.isEmpty()) {
            for (GoodsSaledo entity : rows) {
                entity.setStatus("6");
                entity.setBuyRemain(entity.getBuyRemain().subtract(entity.getBuyCount()));
                goodsSaledoMapper.update(entity);
                GoodsSalebuyall goodsSalebuyall=goodsSalebuyallMapper.selectByGoodsIdAndRemark(entity.getGoodsId(),entity.getRemark(),SecurityUtils.getUsername());
                if(goodsSalebuyall!=null){
                    goodsSalebuyall.setShengyu(entity.getBuyCount().add(goodsSalebuyall.getShengyu()));
                    goodsSalebuyall.setCounts(entity.getBuyCount().add(goodsSalebuyall.getCounts()));
                    goodsSalebuyall.setQinggouId(entity.getId());
                    goodsSalebuyall.setMainId(goodsSaledo.getMainId());
                    if(goodsSalebuyall.getGoodsBuyTotal()!=null && entity.getGoodsBuyTotal()!=null){
                        goodsSalebuyall.setGoodsBuyTotal(entity.getBuyCount().add(goodsSalebuyall.getGoodsBuyTotal()));
                    }
                    if(goodsSalebuyall.getGoodsTotalPrice()!=null && entity.getGoodsTotalPrice()!=null){
                        goodsSalebuyall.setGoodsTotalPrice(entity.getGoodsTotalPrice().add(goodsSalebuyall.getGoodsTotalPrice()));
                    }
                    goodsSalebuyallService.update(goodsSalebuyall);
                }else{
                    goodsSalebuyall=new  GoodsSalebuyall();
                    goodsSalebuyall.setId(IdUtils.fastSimpleUUID());
                    BeanUtils.copyProperties(entity, goodsSalebuyall, "id");
                    goodsSalebuyall.setQinggouId(entity.getId());
                    goodsSalebuyall.setMainId(goodsSaledo.getMainId());
                    goodsSalebuyall.setStatus("6");
                    goodsSalebuyall.setShengyu(entity.getBuyCount());
                    goodsSalebuyall.setCounts(entity.getBuyCount());
                    goodsSalebuyall.setGoodsBuyTotal(entity.getBuyCount());
                    goodsSalebuyall.setGoodsTotalPrice(entity.getBuyCount().multiply(entity.getGoodsBuyPrice()));
                    goodsSalebuyallService.insert(goodsSalebuyall);
                }
                GoodsSalebuy goodsSalebuy =new GoodsSalebuy();
                goodsSalebuy.setId(IdUtils.fastSimpleUUID());
                BeanUtils.copyProperties(entity, goodsSalebuy, "id");
                goodsSalebuy.setQinggouId(entity.getId());
                goodsSalebuy.setMainId(null);
                goodsSalebuy.setCgallid(goodsSalebuyall.getId());
                goodsSalebuy.setStatus("5");
                goodsSalebuy.setShengyu(entity.getBuyCount());
                insert(goodsSalebuy);
            }
        }
    }




    /**
     * 入库明细
     * @param rukumingxi 入库明细
     * @return 结果
     */
    @Override
    @Transactional
    public void insertForReturn(LvRukumingxi rukumingxi) {
        List<LvRukumingxi> mingxiList = rukumingxi.getMingxiList();
            for (LvRukumingxi lvRukumingxi : mingxiList) {
                lvRukumingxi.setCanCount(lvRukumingxi.getCanCount().subtract(lvRukumingxi.getCanCount()));
                lvRukumingxi.setReturnCount(lvRukumingxi.getReturnCountTemp());
                rukumingxiService.update(lvRukumingxi);
                GoodsSalebuyall goodsSalebuyall=goodsSalebuyallMapper.selectByGoodsIdAndRemark(lvRukumingxi.getGoodsId(),lvRukumingxi.getRemark(),SecurityUtils.getUsername());
                if(goodsSalebuyall!=null){
                    goodsSalebuyall.setShengyu(lvRukumingxi.getReturnCountTemp().add(goodsSalebuyall.getShengyu()));
                    goodsSalebuyall.setCounts(lvRukumingxi.getReturnCountTemp().add(goodsSalebuyall.getCounts()));
                    if(lvRukumingxi.getDun()!=null && goodsSalebuyall.getDun()!=null) {
                        goodsSalebuyall.setDun(lvRukumingxi.getDun().add(goodsSalebuyall.getDun()));
                    }
                    goodsSalebuyall.setRukumingxiId(lvRukumingxi.getId());
                    goodsSalebuyall.setGoodsBuyPrice(lvRukumingxi.getGoodsBuyPrice());
                    goodsSalebuyall.setMainId(null);
                    goodsSalebuyall.setSupplierId(lvRukumingxi.getSupplierId());
                    goodsSalebuyall.setSupplierName(lvRukumingxi.getSupplierName());
                    goodsSalebuyall.setGoodsId(lvRukumingxi.getGoodsId());
                    goodsSalebuyall.setGoodsCode(lvRukumingxi.getParams().get("goodsCode")!=null?lvRukumingxi.getParams().get("goodsCode").toString():null);
                    goodsSalebuyall.setGoodModel(lvRukumingxi.getParams().get("goodsModel")!=null?lvRukumingxi.getParams().get("goodsModel").toString():null);
                    goodsSalebuyall.setGoodsName(lvRukumingxi.getParams().get("goodsName")!=null?lvRukumingxi.getParams().get("goodsName").toString():null);
                    goodsSalebuyall.setIsdo(BusinessConstants.STATUS_NO);
                    goodsSalebuyall.setGoodsType(lvRukumingxi.getGoodsType());
                    goodsSalebuyall.setCreateTime(DateUtils.getNowDate());
                    if(lvRukumingxi.getReturnCountTemp()!=null && lvRukumingxi.getGoodsBuyTotal()!=null){
                        BigDecimal totalPrice = lvRukumingxi.getReturnCountTemp().multiply(lvRukumingxi.getGoodsBuyPrice());
                        goodsSalebuyall.setGoodsBuyTotal(goodsSalebuyall.getGoodsBuyTotal().add(totalPrice).negate());
                    }
                    goodsSalebuyall.setSaleDate(lvRukumingxi.getSaleDate());
                    goodsSalebuyallService.update(goodsSalebuyall);
                }else{
                    goodsSalebuyall=new  GoodsSalebuyall();
                    goodsSalebuyall.setId(IdUtils.fastSimpleUUID());
                    goodsSalebuyall.setSaleDate(lvRukumingxi.getSaleDate());
                    goodsSalebuyall.setRukumingxiId(lvRukumingxi.getId());
                    goodsSalebuyall.setMainId(null);
                    goodsSalebuyall.setStatus("5");
                    goodsSalebuyall.setSupplierId(lvRukumingxi.getSupplierId());
                    goodsSalebuyall.setSupplierName(lvRukumingxi.getSupplierName());
                    goodsSalebuyall.setGoodsId(lvRukumingxi.getGoodsId());
                    goodsSalebuyall.setGoodsCode(lvRukumingxi.getParams().get("goodsCode")!=null?lvRukumingxi.getParams().get("goodsCode").toString():null);
                    goodsSalebuyall.setGoodModel(lvRukumingxi.getParams().get("goodsModel")!=null?lvRukumingxi.getParams().get("goodsModel").toString():null);
                    goodsSalebuyall.setGoodsName(lvRukumingxi.getParams().get("goodsName")!=null?lvRukumingxi.getParams().get("goodsName").toString():null);
                    goodsSalebuyall.setIsdo(BusinessConstants.STATUS_NO);
                    goodsSalebuyall.setGoodsType(lvRukumingxi.getGoodsType());
                    goodsSalebuyall.setShengyu(lvRukumingxi.getReturnCountTemp());
                    goodsSalebuyall.setCounts(lvRukumingxi.getReturnCountTemp());
                    goodsSalebuyall.setCreateTime(DateUtils.getNowDate());
                    goodsSalebuyall.setGoodsBuyPrice(lvRukumingxi.getGoodsBuyPrice().negate());
                    goodsSalebuyall.setGoodsBuyTotal(lvRukumingxi.getReturnCountTemp().multiply(lvRukumingxi.getGoodsBuyPrice()).negate());
                    goodsSalebuyall.setGoodsTotalPrice(lvRukumingxi.getReturnCountTemp().multiply(lvRukumingxi.getGoodsBuyPrice()).negate());
                    goodsSalebuyallService.insert(goodsSalebuyall);
                }
                GoodsSalebuy goodsSalebuy =new GoodsSalebuy();
                goodsSalebuy.setId(IdUtils.fastSimpleUUID());
                goodsSalebuy.setRukumingxiId(lvRukumingxi.getId());
                goodsSalebuy.setGoodsId(lvRukumingxi.getGoodsId());
                goodsSalebuy.setGoodsCode(lvRukumingxi.getParams().get("goodsCode")!=null?lvRukumingxi.getParams().get("goodsCode").toString():null);
                goodsSalebuy.setGoodModel(lvRukumingxi.getParams().get("goodsModel")!=null?lvRukumingxi.getParams().get("goodsModel").toString():null);
                goodsSalebuy.setGoodsName(lvRukumingxi.getParams().get("goodsName")!=null?lvRukumingxi.getParams().get("goodsName").toString():null);
                goodsSalebuy.setIsdo(BusinessConstants.STATUS_NO);
                goodsSalebuy.setMainId(null);
                goodsSalebuy.setRukumingxiId(lvRukumingxi.getId());
                goodsSalebuy.setCgallid(goodsSalebuyall.getId());
                goodsSalebuy.setSupplierId(lvRukumingxi.getSupplierId());
                goodsSalebuy.setSupplierName(lvRukumingxi.getSupplierName());
                goodsSalebuy.setStatus("5");
                goodsSalebuy.setShengyu(lvRukumingxi.getReturnCountTemp());
                goodsSalebuy.setSaleDate(lvRukumingxi.getSaleDate());
                goodsSalebuy.setGoodsBuyPrice(lvRukumingxi.getGoodsBuyPrice().negate());
                goodsSalebuy.setGoodsUnitPrice(lvRukumingxi.getGoodsBuyPrice().negate());
                goodsSalebuy.setGoodsBuyTotal(lvRukumingxi.getReturnCountTemp().multiply(lvRukumingxi.getGoodsBuyPrice()).negate());
                insert(goodsSalebuy);
            }
    }







}
